package com.bigknow.appstore.web.base.dao.impl;

import com.bigknow.appstore.web.base.dao.IAuditLogDao;
import com.bigknow.appstore.web.framework.dao.AbstractDao;
import com.bigknow.appstore.web.base.entity.AuditLog;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * Created by TwelveFoldEr on 2016/3/23.
 */
@Component
public class AuditLogDao extends AbstractDao<AuditLog> implements IAuditLogDao {

    @Override
    public List<AuditLog> findByDateRange(String start, String end) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDateTime = LocalDateTime.parse(start, dateTimeFormatter);
        LocalDateTime endDateTime = LocalDateTime.parse(end, dateTimeFormatter);
        ZoneOffset zo = ZoneOffset.ofHours(8);
        Date startDate = Date.from(startDateTime.toInstant(zo));
        Date endDate = Date.from(endDateTime.toInstant(zo));
        return datastore.find(AuditLog.class).filter("operationTime >=", startDate)
                .filter("operationTime <=", endDate).asList();
    }
}
